{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": [
     "HD"
    ]
   },
   "source": [
    "### **一、算术运算**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "31.0"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1+(100-20)/4+5*2 #四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1024"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2**10 #乘方运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "7%5 #求模运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "7//5 #地板除法(整除)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abs(-1) #绝对值运算(适用于float、int和复数类型。返回是float和int类型)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.fabs(-5) #绝对值运算(只适用于float、int类型。返回是float类型)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.sin(math.pi/2) #三角函数运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.log(math.e) #对数运算(log默认以e为底)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.factorial(5) # 阶乘运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4+1j)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1 + 2j) + (3 - 1j) #复数运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1+0j)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "complex(2,1)/(2+1j) #复数运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#math.factorial(1000) + 2**100 #强大的计算器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": [
     "HD"
    ],
    "toc-hr-collapsed": true
   },
   "source": [
    "\n",
    "### 二、输入输出"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "输入：input\n",
    "\n",
    "输出：print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "please input your weight: 60\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'60'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#input 输入的都是字符串\n",
    "weight = input('please input your weight:')\n",
    "weight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "please input your weight: 60\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "60.0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#利用float函数将字符串转换成浮点数\n",
    "weight = float(input('please input your weight:'))\n",
    "weight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "please input your age: 2020-1998\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "22"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#利用eval函数将输入的字符串作为表达式进行计算\n",
    "age = eval(input('please input your age:'))\n",
    "age #算年龄"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'your weight is:60kg'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#输出用print,print()里变量用逗号(,)隔开即有：默认的 sep=' '分隔符，end='\\n'结尾符\n",
    "print(f'your weight is:{weight}kg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'your age is:22'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#或者直接使用f-string(python3.6及以上版本)\n",
    "f'your age is:{age}'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "\n",
    "### 三、导入模块"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "import ...\n",
    "\n",
    "或 from ... import ...\n",
    "或 import ... as ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2020, 4, 12)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 以普通方式导入\n",
    "\n",
    "import datetime\n",
    "datetime.datetime.today() #获取当前时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2020, 4, 12, 1, 16, 44, 79453)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 导入模块中某个对象\n",
    "\n",
    "from datetime import datetime\n",
    "datetime.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-04-12 01:54:03.982064\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "datetime.date(2020, 4, 12)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 导入模块中全部对象\n",
    "\n",
    "from datetime import *\n",
    "print(datetime.today())\n",
    "date.today() #获取当前日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2020, 4, 12, 1, 20, 44, 994886)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 以简写方式导入模块\n",
    "\n",
    "import datetime as dt\n",
    "dt.datetime.today()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "\n",
    "### 四、语法规则"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "1、标识符\n",
    "\n",
    "标识符由字母、数字、下划线组成，区分大小写，不能以数字开头。\n",
    "以下划线开头的标识符有特殊含义。以单下划线开头的(_foo)代表不能直接访问类属性，以双下划线开头的(__foo)代表类的私有成员；以双下划线开头和结尾的(__foo__)代表Python里特殊方法专用的标识，如__init__()代表类的构造函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.18.2'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.__version__"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "2、缩进\n",
    "\n",
    "Python的代码块不使用大括号或者其他来控制类、函数、以及其他逻辑判断，而是使用缩进来实现代码分组。通常用四个空格来进行缩进。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "a, b = 1, 2\n",
    "if a > b:\n",
    "    x = a\n",
    "else:\n",
    "    x = b\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "3、注释\n",
    "\n",
    "Python中单行注释采用 # 开头。多行注释在代码块首尾分别使用三个单引号(''')或三个双引号(\"\"\")。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "author: HD\n",
    "date: 2020-4-12\n",
    "\"\"\"\n",
    "def my_abs(x): #自定义绝对值函数\n",
    "    if x > 0:\n",
    "        return x\n",
    "    else:\n",
    "        return -x\n",
    "my_abs(-5)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "4、一条语句粉多行显示\n",
    "\n",
    "Python语句中一般以新行作为语句的结束符。\n",
    "但是我们可以使用反斜杠(\\)将一行的语句分为多行显示。\n",
    "如果有{},[],()跨行则可以不使用\\。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(1+2+3+4+5+6+\\\n",
    "+7+8+9+10)\n",
    "a = [1,2,3,4,\n",
    "    5,6,7,8]\n",
    "a"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "5、同一行显示多条语句\n",
    "\n",
    "Python可以在同一行中使用多条语句，语句之间使用分号分割。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 6\n"
     ]
    }
   ],
   "source": [
    "a = 1; b = a + 5\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": [
     "HD"
    ]
   },
   "source": [
    "\n",
    "### 五、Python数据结构概述"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python内建的数据结构有列表，元组，字符串，集合，字典等。\n",
    "\n",
    "此外常用的还有numpy中的`array`，以及pandas中的`dataframe`和`series`。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1、有序数据结构：**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`list`(列表)，是有序集合，没有固定大小，可以通过对偏移量以及其他方法修改列表大小。基本形式如：[1,2,3,4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`tuple`(元组)，是有序集合、**不可变的**，可以进行组合和赋值运算后会生成一个新的元组。基本形式如：(1,3,5,7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`str`(字符串)，是有序集合，基本形式如：'hello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[9, 2, 3, 4]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#有序数据结构可通过下标访问数据\n",
    "l = [1,2,3,4]\n",
    "print(l[0]) \n",
    "l[0] = 9\n",
    "l"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2、无序数据结构**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`set`(集合)，是一个无序**不重复**元素的集合。基本功能包括关系运算和消除重复元素。基本形式如：{'apple','orange','banana'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`dict`(字典)，是无序的键值对的集合。**键必须是互不相同的(在同一个字典之内)**，可通过键访问其值。基本形式如：{'ICBC':95588,'BOC':95566}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "95566"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'ICBC':95588,'BOC':95566}\n",
    "d['BOC']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**3、小知识：浅拷贝和深拷贝**\n",
    "\n",
    "浅拷贝(不可变类型tuple利用赋值):只是原来对象的一个别名即做更改拷贝后的对象会改变原来的对象\n",
    "\n",
    "深拷贝(可变类型利用copy()函数):会开辟新的内存空间即做更改拷贝后的对象不会改变原来的对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#### `列表list`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'orange', 'banana']\n",
      "[1, 3, 5, 7, 9]\n",
      "[1, 2, 4, 8, 16]\n",
      "[[0, 1, 2], [1, 2, 3]]\n"
     ]
    }
   ],
   "source": [
    "### 1、建立列表\n",
    "\n",
    "fruits = ['apple','orange','banana'] #直接建立\n",
    "a = list(range(1,11,2)) #利用list建立\n",
    "b = [2**i for i in range(5)] #利用列表推导式(for循环内的语句写在前面)建立\n",
    "c = [[i+j for i in range(3)] for j in range(2)] #利用列表推导式建立二维列表\n",
    "print(fruits,a,b,c,sep='\\n') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,5\n",
      "[1, 3]\n",
      "[1, 3]\n",
      "[1, 2, 3, 4, 5]\n",
      "[1, 2, 3, 4]\n",
      "[1, 3, 5]\n"
     ]
    }
   ],
   "source": [
    "### 2、访问列表\n",
    "# 列表支持下标访问和切片访问---具体有顺序(从左往右)和逆序(从右往左)\n",
    "# 比如：顺序从0开始，逆序从-1开始\n",
    "\n",
    "l = [1,2,3,4,5]\n",
    "print(l[0],l[-1],sep=',') #下标访问\n",
    "#切片访问，中间用冒号隔开，前两位取值遵循左闭右开原则，第三位2表示步长\n",
    "print(l[0:3:2],l[-5:-2:2],sep='\\n') \n",
    "print(l[:])\n",
    "print(l[:-1])\n",
    "print(l[::2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 3, 4, 5]\n",
      "[0, 3, 4, 5]\n",
      "[0, 3, 4, 5, 0, 3, 4, 5, 0, 3, 4, 5]\n",
      "[0, 3, 4, 5, 0, 3, 4, 5, 0, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "### 3、修改列表\n",
    "\n",
    "l = [1,2,3,4,5]\n",
    "l[0] = 0\n",
    "print(l)\n",
    "l.remove(2) #或者用del(l[1])删除元素值2\n",
    "print(l)\n",
    "l = l * 3 # *号有复制拷贝多份效果\n",
    "print(l)\n",
    "l = l + [6,7,8,9] # +号对序列有拼接作用\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16\n",
      "9\n",
      "0\n",
      "[0, 0, 0, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 7, 8, 9]\n",
      "[9, 8, 7, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 0, 0, 0]\n"
     ]
    }
   ],
   "source": [
    "### 4、列表常用函数\n",
    "\n",
    "print(len(l)) #列表长度\n",
    "\n",
    "print(max(l)) #列表中最大值\n",
    "\n",
    "print(min(l)) #列表中最小值\n",
    "\n",
    "print(sorted(l)) #列表中元素排序，默认从小到大\n",
    "print(sorted(l,reverse=True)) #改为逆序(从大到小)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 8, 9, 10, 11, 12]\n",
      "[1, 7, 8, 9, 10, 11, 12]\n"
     ]
    }
   ],
   "source": [
    "### 5、列表常用方法\n",
    "\n",
    "lt = [7,8,9]\n",
    "lt.extend([10,11])\n",
    "lt.append(12)\n",
    "print(lt)\n",
    "lt.insert(0,1)\n",
    "print(lt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#### `字典dict`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "字典在插入元素和查找元素方面很多时候比列表更加高效。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 3, 2: 4, 3: 6}\n",
      "{'a': 1, 'b': 2}\n",
      "{'x': 1, 'y': 2}\n",
      "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}\n"
     ]
    }
   ],
   "source": [
    "### 1、创建字典\n",
    "\n",
    "d = {1:3,2:4,3:6} #一般创建\n",
    "a = dict([('a',1),['b',2]]) #利用dict创建\n",
    "b = dict(x=1,y=2)\n",
    "D = {i:i**2 for i in range(5)} #利用字典推导式创建\n",
    "print(d,a,b,D,sep='\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 3, 'b': 2, 'c': 5}\n",
      "{'a': 3, 'b': 4, 'c': 5, 'd': 8}\n"
     ]
    }
   ],
   "source": [
    "### 2、字典常用操作\n",
    "\n",
    "a['a'] = 3 #通过键来改变和添加对应的值\n",
    "a['c'] = 5\n",
    "print(a)\n",
    "a.update({'b':4,'d':8}) #通过update函数更新对应的键值\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['a', 'b', 'c'])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values([3, 2, 5])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('a', 3), ('b', 2), ('c', 5)])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "{'a': 3, 'b': 4, 'c': 5, 'd': 8}\n",
      "{'a': 3, 'b': 4, 'c': 5, 'd': 8, 'e': 4}\n"
     ]
    }
   ],
   "source": [
    "d = {'a':3,'b':4,'c':5,'d':8}\n",
    "print(d.get('b')) #获取键对应的值\n",
    "d.get('e',0) #给没有的键设置默认值，没有添加进去\n",
    "print(d)\n",
    "d.setdefault('e',4) #给没有的键设置默认值并添加进去\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#### `字符串str`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12345\n",
      "I'm Clark.\n",
      "I just use Python to say:\"hello world\"!\n"
     ]
    }
   ],
   "source": [
    "### 1、创建字符串\n",
    "\n",
    "print(str(12345))\n",
    "s1 = 'I\\'m Clark.\\nI just use Python to say:\"hello world\"!' #\\为转义字符\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm Clark.\n",
      "I just use Python to say:\"hello world\"!\n"
     ]
    }
   ],
   "source": [
    "s2 = \"I'm Clark.\\nI just use Python to say:\\\"hello world\\\"!\"\n",
    "print(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm Clark.\n",
      "I just use Python to say:\"hello world\"!\n",
      "\n"
     ]
    }
   ],
   "source": [
    "s3 = '''I'm Clark.\n",
    "I just use Python to say:\"hello world\"!\n",
    "'''\n",
    "print(s3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm Clark.\n",
      "I love Python.\n",
      "I just use Python to say:\"hello world\"!\n"
     ]
    }
   ],
   "source": [
    "### 2、字符串拼接(+,join)\n",
    "\n",
    "s1 = \"I'm Clark.\"\n",
    "s2 = \"I love Python.\"\n",
    "s3 = 'I just use Python to say:\"hello world\"!'\n",
    "print(s1+'\\n'+s2+'\\n'+s3) # +号拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm Clark.\n",
      "I love Python.\n",
      "I just use Python to say:\"hello world\"!\n"
     ]
    }
   ],
   "source": [
    "print('\\n'.join([s1,s2,s3])) #join拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abcabcabcabcabc'"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'abc' * 5 #乘法有复制效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Clark', '22', 'male']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['Clark', '22\\tmale']"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 3、字符串的常用方法\n",
    "\n",
    "#split()中第一个参数：分隔符--默认为所有的空字符，包括空格、换行(\\n)、制表符(\\t)等；第二个参数：分割次数--默认为 -1, 即分隔所有\n",
    "print(\"Clark\\t22\\tmale\".split())\n",
    "\"Clark\\t22\\tmale\".split('\\t',1) #分割完后的字符串个数=分割次数+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'123abc'"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\\n123abc\\t\".strip() #strip()方法用于移除字符串头尾指定的字符（默认为空格或换行符）或字符序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abcdefxyz'"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"$$$$abcdef$$$xyz\".replace('$','') #用空字符串代替$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 4、格式化字符串(%或format或f-string)\n",
    "\n",
    "age = 22\n",
    "weight = 60.5\n",
    "hobby = 'travelling'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm 22 years old. My weight is 60.50 kg. I like travelling.\n"
     ]
    }
   ],
   "source": [
    "fs = \"I'm %d years old. My weight is %.2f kg. I like %s.\" %(age,weight,hobby) # %格式化\n",
    "print(fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm 22 years old. My weight is 60.50 kg. I like travelling.\n"
     ]
    }
   ],
   "source": [
    "fs = \"I'm {} years old. My weight is {:.2f} kg. I like {}.\" .format(age,weight,hobby) # format格式化\n",
    "print(fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm 22 years old. My weight is 60.50 kg. I like travelling.\n"
     ]
    }
   ],
   "source": [
    "fs = f\"I'm {age} years old. My weight is {weight:.2f} kg. I like {hobby}.\" # f-string格式化(python3.6版本及以上可以使用)\n",
    "print(fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#### `元组tuple`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "()\n",
      "(1,)\n",
      "(1, 2)\n"
     ]
    }
   ],
   "source": [
    "### 1、创建元组\n",
    "\n",
    "t = (1,2,3)\n",
    "t0 = ()\n",
    "t1 = (1,)\n",
    "x = 1,2\n",
    "print(t,t0,t1,x,sep='\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "('a', 'b', 'c')\n"
     ]
    }
   ],
   "source": [
    "y = tuple([1,2,3])\n",
    "z = tuple('abc')\n",
    "print(y,z,sep='\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "### 2、使用元组\n",
    "\n",
    "t = (1,2,2,1,1,3)\n",
    "print(t.count(1))\n",
    "print(t.index(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "#序列解包\n",
    "a,b = 1,2\n",
    "t1,t2,t3 = [1,2,3]\n",
    "print(a,b,t1,t2,t3,sep='\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{(1, 2): 4}\n"
     ]
    }
   ],
   "source": [
    "#元组可以作为字典的key\n",
    "\n",
    "d ={(1,2):4}\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `集合set`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3}\n",
      "set()\n",
      "{0, 1, 4, 9, 16}\n"
     ]
    }
   ],
   "source": [
    "### 1、创建集合\n",
    "\n",
    "s = {1,2,3}\n",
    "s0 = set() #创建空集合，{}是创建一个空字典\n",
    "s1 = {x**2 for x in range(5)}\n",
    "print(s,s0,s1,sep='\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6}"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 2、使用集合\n",
    "\n",
    "# 去除重复对象，求交集、并集、补集等操作\n",
    "a = [1,2,3,6,4,5,5,2,6,3,1]\n",
    "set(a) #去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 5, 6}\n",
      "{1, 2, 3, 5, 6, 7, 8}\n",
      "{1, 3}\n"
     ]
    }
   ],
   "source": [
    "s1 = {1,2,3,5,6}\n",
    "s2 = {2,5,6,7,8}\n",
    "print(s1 & s2) #交集\n",
    "print(s1 | s2) #并集\n",
    "print(s1.difference(s2)) #补集"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 六、条件语句 if"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "### 1、if语句\n",
    "a,b = 3,2\n",
    "\n",
    "if a>b:\n",
    "    x = a\n",
    "else:\n",
    "    x = b\n",
    "print(x)\n",
    "\n",
    "#if...elif...elif...else...\n",
    "\n",
    "#单行实现if语句\n",
    "y = a if a>b else b\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 203,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 2、逻辑运算符\n",
    "\n",
    "2>3 \n",
    "1+2<100\n",
    "3 == 4-1\n",
    "\n",
    "2 != 3\n",
    "not 2 == 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 3、and和or\n",
    "\n",
    "2>3 and 4<5\n",
    "2>3 or 4<5\n",
    "\n",
    "# 短路计算\n",
    "[] and [1,2] and {}\n",
    "[] or [1,2] or {} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(False, False, False, False, False, False)"
      ]
     },
     "execution_count": 216,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 注意空字符串、空列表、空字典、空元组和空集合以及0的bool值为false\n",
    "bool(''),bool([]),bool({}),bool(()),bool(set()),bool(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 七、循环语句 for,while"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 5 6 "
     ]
    }
   ],
   "source": [
    "### 1、for循环\n",
    "\n",
    "l = [1,2,3,4,5,6]\n",
    "for i in l:\n",
    "    print(i,end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a : 1\n",
      "b : 2\n",
      "c : 3\n"
     ]
    }
   ],
   "source": [
    "d = {'a':1,'b':2,'c':3}\n",
    "for k,v in d.items():\n",
    "    print(k,':',v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 x 1 =  1  \n",
      "1 x 2 =  2  2 x 2 =  4  \n",
      "1 x 3 =  3  2 x 3 =  6  3 x 3 =  9  \n",
      "1 x 4 =  4  2 x 4 =  8  3 x 4 = 12  4 x 4 = 16  \n",
      "1 x 5 =  5  2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25  \n",
      "1 x 6 =  6  2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  6 x 6 = 36  \n",
      "1 x 7 =  7  2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  6 x 7 = 42  7 x 7 = 49  \n",
      "1 x 8 =  8  2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  6 x 8 = 48  7 x 8 = 56  8 x 8 = 64  \n",
      "1 x 9 =  9  2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  6 x 9 = 54  7 x 9 = 63  8 x 9 = 72  9 x 9 = 81  \n"
     ]
    }
   ],
   "source": [
    "# for循环实现九九乘法表\n",
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print(f'{j} x {i} = {i*j:2d}',end='  ')\n",
    "    print() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050 101\n"
     ]
    }
   ],
   "source": [
    "### 2、while循环\n",
    "\n",
    "# 求1+2+3+...+100的和\n",
    "s,i = 0,1\n",
    "while i<=100:\n",
    "    s += i\n",
    "    i += 1\n",
    "print(s,i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1 2 3 5 8 13 21 34 55 89 "
     ]
    }
   ],
   "source": [
    "# 打印斐波那契(Fibonacci)数列。这个数列前两项为 1，之后的每一个项都是前两项之和。\n",
    "a, b = 0, 1\n",
    "while b < 100:\n",
    "    print(b,end=' ')\n",
    "    a, b = b, a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 x 1 =  1  \n",
      "1 x 2 =  2  2 x 2 =  4  \n",
      "1 x 3 =  3  2 x 3 =  6  3 x 3 =  9  \n",
      "1 x 4 =  4  2 x 4 =  8  3 x 4 = 12  4 x 4 = 16  \n",
      "1 x 5 =  5  2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25  \n",
      "1 x 6 =  6  2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  6 x 6 = 36  \n",
      "1 x 7 =  7  2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  6 x 7 = 42  7 x 7 = 49  \n",
      "1 x 8 =  8  2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  6 x 8 = 48  7 x 8 = 56  8 x 8 = 64  \n",
      "1 x 9 =  9  2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  6 x 9 = 54  7 x 9 = 63  8 x 9 = 72  9 x 9 = 81  \n"
     ]
    }
   ],
   "source": [
    "# while循环实现九九乘法表\n",
    "i = 1\n",
    "while i<10:\n",
    "    j = 1\n",
    "    while j<i+1:\n",
    "        print(f'{j} x {i} = {i*j:2d}',end='  ')\n",
    "        j +=1\n",
    "    print()\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "helloworld"
     ]
    }
   ],
   "source": [
    "### 3、循环控制 continue，break\n",
    "\n",
    "s = 'hello world'\n",
    "\n",
    "# break跳出本层循环\n",
    "for i in s:\n",
    "    if i == ' ': #遇到空格就终止\n",
    "        break\n",
    "    print(i,end='')\n",
    "\n",
    "print() #换行\n",
    "\n",
    "# continue跳出本次循环\n",
    "for i in s:\n",
    "    if i == ' ':  #不打印空格\n",
    "        continue\n",
    "    print(i,end='')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 2 3 4 Bye bye\n"
     ]
    }
   ],
   "source": [
    "### 4、循环中的else\n",
    "\n",
    "# 循环后面使用可选的 else 语句。它将会在循环完毕后执行，一旦有break终止就不会执行else后面的语句。\n",
    "for i in range(0, 5):\n",
    "    print(i,end=' ')\n",
    "else:\n",
    "    print(\"Bye bye\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Python中循环的 else 子句给我们提供了检测循环是否顺利执行完毕的一种优雅方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 八、函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 1、函数参数：普通参数，默认参数，可变参数，关键字参数\n",
    "### 参数定义顺序：普通参数，默认参数，可变参数，关键字参数\n",
    "# 普通参数 x(位置参数)\n",
    "\n",
    "def my_abs(x):\n",
    "    return x if x>=0 else -x\n",
    "\n",
    "my_abs(-5)\n",
    "my_abs(x = -5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "125"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 默认参数 n(参数缺失时赋默认参数且默认参数定义时必须写在普通参数后)\n",
    "\n",
    "def my_power(x,n=2):\n",
    "    return x**n\n",
    "\n",
    "my_power(5)\n",
    "my_power(5,3)\n",
    "my_power(x=5,n=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可变参数 *args(可以传入不定长度的参数序列---元组或列表)\n",
    "\n",
    "def my_sum(*args):\n",
    "    s = 0\n",
    "    for i in args:\n",
    "        s += i\n",
    "    return s\n",
    "\n",
    "my_sum()\n",
    "my_sum(1,2,3)\n",
    "my_sum(*(1,2,3))\n",
    "my_sum(*[1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Clark', 'age': 22, 'grade': '一班', 'hometown': 'hunan'}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 关键字参数 **kv(可以传入字典)\n",
    "\n",
    "def student(name,age,**kv):\n",
    "    d = {'name':name,'age':age}\n",
    "    d.update(kv) #将传进的**kv参数更新到d中\n",
    "    return d\n",
    "\n",
    "student('HD',22,grade=\"一班\",gender='male')\n",
    "Clark = {'name':'Clark','age':22,'grade':\"一班\",'hometown':'hunan'}\n",
    "student(**Clark)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "89"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 2、递归函数\n",
    "# 递归函数特点：调用自身\n",
    "\n",
    "# 斐波那契(Fibonacci)数列。这个数列前两项为 1，之后的每一个项都是前两项之和。\n",
    "def fib(n):\n",
    "    if n in [1,2]: #前两项为 1\n",
    "        return 1\n",
    "    else:\n",
    "        return fib(n-1) + fib(n-2)\n",
    "\n",
    "fib(11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 3、装饰器\n",
    "\n",
    "# 装饰器在不更改函数代码前提下赋予函数额外的功能\n",
    "# 通常把函数非核心逻辑如插入日志、权限校验、性能测试放在装饰器中\n",
    "\n",
    "import time\n",
    "def decorater(func):\n",
    "    def wrapper(*args,**kv):\n",
    "        tic = time.time()\n",
    "        ans = func(*args,**kv)\n",
    "        toc = time.time()\n",
    "        print(f'{func.__name__} is called. {toc-tic}s are used.')\n",
    "        return ans\n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "@decorater\n",
    "def my_sum(*args):\n",
    "    s = 0\n",
    "    for i in args:\n",
    "        s += i\n",
    "    return s\n",
    "# @decorater是一个语法糖\n",
    "# 相当于 my_sum = decorater(my_sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my_sum is called. 0.0s are used.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "4950"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_sum(*range(100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my_sum is called. 0.0s are used.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_sum(1,2,3) #相当于 decorater(my_sum)(1,2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fib is called. 0.0s are used.\n",
      "fib is called. 0.0s are used.\n",
      "fib is called. 0.0s are used.\n",
      "fib is called. 0.0s are used.\n",
      "fib is called. 0.0s are used.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@decorater\n",
    "def fib(n):\n",
    "    if n in [1,2]: #前两项为 1\n",
    "        return 1\n",
    "    else:\n",
    "        return fib(n-1) + fib(n-2)\n",
    "\n",
    "fib(4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 九、lambda匿名函数\n",
    "\n",
    "lambda只是一个表达式，适合定义较为简单的函数,不可在里面写循环。  \n",
    "\n",
    "lambda函数的定义语法是：\n",
    "\n",
    "**fun = `lambda` 参数序列:返回值表达式**\n",
    "\n",
    "一般来说通过使用lambda匿名函数可以节约程序开支并加快运行速度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_abs = lambda x:x if x>=0 else -x\n",
    "my_abs(-5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_power = lambda x,n=2:x**n\n",
    "my_power(-5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-125"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(lambda x,n=2:x**n)(-5,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_sum = lambda *args:sum(args)\n",
    "my_sum(1,2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'HD', 'age': 22, 'grade': '一班', 'gender': 'male'}"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "student = lambda name,age,**kv:dict(name = name,age = age,**kv)\n",
    "student('HD',22,grade='一班',gender='male')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "89"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fib = lambda n:1 if n in [1,2] else fib(n-1) + fib(n-2)\n",
    "fib(11)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 十、高阶函数\n",
    "\n",
    "以函数为参数的函数称为高阶函数。常用的内置高阶函数有：map,reduce,filter。\n",
    "\n",
    "高阶函数和匿名函数搭配使用，堪称绝配。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16]"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 1、map将一个函数的方法作用到一个序列或者多个序列，且map返回的是map object\n",
    "# map原意为：映射\n",
    "\n",
    "list(map(lambda x:x**2,[1,2,3,4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a1', 'b2', 'c3']"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(lambda x,y:x+y,'abc','123')) #字符串中+号是拼接作用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 2、reduce将一个带有两个参数的函数的方法依次迭代作用到一个序列\n",
    "# reduce原意为：减少\n",
    "# reduce(f,[a,b,c,d]) 相当于 f(f(f(a,b),c),d)\n",
    "\n",
    "from functools import reduce\n",
    "reduce(lambda x,y:x+y,[1,2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 3、filter根据一个函数(函数返回值最好是bool值)的规则过滤序列中的元素，且filter返回值返回的是filter object\n",
    "# filter原意为：过滤\n",
    "\n",
    "list(filter(lambda x:x>0,[-1,1,-2,2]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 十一、Python推导式\n",
    "\n",
    "Python中的推导式是我最喜爱的一类语法规则，没有之一。\n",
    "\n",
    "Python推导式可以生成列表、字典和集合。\n",
    "\n",
    "Python推导式虽然简单，但表达能力很强，可以实现map，filter等功能，并且可以多重遍历。\n",
    "\n",
    "淋漓尽致地体现了Python语言的simple，readable，powerful的特点。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Jim', 'Mary'), ('Jim', 'Lily'), ('John', 'Mary'), ('John', 'Lily')]"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 1、列表推导式\n",
    "\n",
    "# 语法规范：\n",
    "# out_list = [out_express for out_express in input_list if out_express_condition]\n",
    "# 其中的 if 条件判断根据需要可有可无。\n",
    "\n",
    "# 生成平方数序列\n",
    "[x**2 for x in range(5)]\n",
    "\n",
    "# 求序列的绝对值\n",
    "l = [-1,-2,3,5,-6]\n",
    "[x if x>=0 else -x for x in l] # 函数在循环前\n",
    "list(map(lambda x:x if x>=0 else -x,l))\n",
    "\n",
    "# 过滤序列中某些元素(条件在循环后)\n",
    "[x for x in l if x>0]\n",
    "list(filter(lambda x:x>0,l))\n",
    "\n",
    "# 多重遍历\n",
    "girls = ['Mary','Lily']\n",
    "boys = ['Jim','John']\n",
    "[(b,g) for b in boys for g in girls] #out_express需用括号，顺序和不用推导式的原始for循环是一致的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 'Winter', 2: 'Summer', 3: 'Autumn', 4: 'Spring'}"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 2、字典推导式\n",
    "\n",
    "seasons = {'Spring','Summer','Autumn','Winter'}\n",
    "{k:v for k,v in enumerate(seasons,start=1)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 'Winter'), (2, 'Summer'), (3, 'Autumn'), (4, 'Spring')]"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(enumerate(seasons,start=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'b': 2, 'c': 3}"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "keys = ['a','b','c']\n",
    "values = [1,2,3]\n",
    "{k:v for k,v in zip(keys,values)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('a', 1), ('b', 2), ('c', 3)]"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(keys,values))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2, 4}"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 3、集合推导式\n",
    "\n",
    "# 生成绝对值序列\n",
    "{abs(x) for x in [-1,2,-2,-3,1]}\n",
    "\n",
    "# 求两个集合的交集\n",
    "a = {1,2,3,4}\n",
    "b = {2,4,6,8}\n",
    "\n",
    "a.intersection(b) #内置方法求交集\n",
    "{x for x in a if x in b} #集合推导式求交集"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 十二、类和对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1、面向对象基本概念\n",
    "\n",
    "- 什么是面向过程编程和面向对象编程？\n",
    "\n",
    "面向过程编程(POP：Process Oriented Programming).程序被看成一系列命令的依次执行。基本封装形式为函数。\n",
    "设计函数的基本要点是IPO：输入input——>处理Process——>输出Output.\n",
    "\n",
    "面向对象编程(OOP:Object Oriented Programming).程序被看成一系列对象的相互作用。基本的封装形式是类。\n",
    "设计类的基本要点是RPM：关系Relation，属性Property，方法Method.\n",
    "\n",
    "- 面向对象基本术语？\n",
    "\n",
    "类：class，抽象数据结构，数据和算法的封装。如：定义一个类：dog。\n",
    "\n",
    "对象：object，类的实例。如：dog类的一个实例：点点dot。\n",
    "\n",
    "属性：properties，和对象关联的数据部分。如：weight体重，breed品种。\n",
    "\n",
    "方法：methods，和对象关联的算法部分。如：run(),eat(),bark()。\n",
    "\n",
    "- 面向对象编程的优点？\n",
    "\n",
    "容易使用：封装，奇妙的句点符号。\n",
    "\n",
    "容易扩展：继承，多态。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2、创建类和对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Husky\n",
      "giao is running...\n",
      "Bowwow,Bowwow,Bowwow...\n",
      "giao is eating meat...\n",
      "I think I'm a hero and very handsome!\n",
      "HaHaHa!!!\n",
      "Zzz...Zzz...Zzz...\n"
     ]
    }
   ],
   "source": [
    "# 创建一个Dog类\n",
    "class Dog(object):\n",
    "    \n",
    "    # 类的构造函数__init__\n",
    "    def __init__(self,name,weight,breed,age):\n",
    "        self.name = name\n",
    "        self.weight = weight\n",
    "        self.breed = breed\n",
    "        \n",
    "        # __age为私有属性\n",
    "        self.__age = age\n",
    "    \n",
    "    def run(self):\n",
    "        print(f'{self.name} is running...')\n",
    "        \n",
    "    def bark(self):\n",
    "        print('Bowwow,Bowwow,Bowwow...')\n",
    "        \n",
    "    def eat(self,food):\n",
    "        print(f'{self.name} is eating {food}...')\n",
    "        \n",
    "    def sleep(self):\n",
    "        print('Zzz...Zzz...Zzz...')\n",
    "        \n",
    "    # __think为私有方法\n",
    "    def __think(self):\n",
    "        print(\"I think I'm a hero and very handsome!\")\n",
    "        \n",
    "    # speak公有方法可以调用私有方法\n",
    "    def speak(self,words=''):\n",
    "        self.__think()\n",
    "        if words:\n",
    "            print(words)\n",
    "            \n",
    "# 实例化一个对象\n",
    "gu = Dog('giao',3,'Husky',4) #调用类的构造函数__init__\n",
    "\n",
    "# 调用公有属性和公有方法\n",
    "print(gu.breed)\n",
    "gu.run()\n",
    "gu.bark()\n",
    "gu.eat('meat')\n",
    "gu.speak(\"HaHaHa!!!\")\n",
    "gu.sleep()\n",
    "\n",
    "# 私有属性和私有方法不能够直接在类外部访问\n",
    "# gu.__age\n",
    "# gu.__think()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3、获取对象信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Python中万物皆对象。对象由类创建而来，所有的类都是object基类的子类。\n",
    "\n",
    "# type查看对象类别\n",
    "type(1),type(False),type(gu),type(len),type([1,2,3])\n",
    "\n",
    "# isinstance 测试某个对象是否属于某个类\n",
    "isinstance({1,2,3},set) #输出：True\n",
    "isinstance(max,object) #输出：True\n",
    "isinstance(123,(list,str,float,int))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 十三、封装和继承\n",
    "\n",
    "1、封装\n",
    "\n",
    "奇妙的句点符号：通过奇妙的句点符号可以召唤对象的属性和方法。\n",
    "私有属性和私有方法除外。\n",
    "\n",
    "(1) 公有属性和公有方法\n",
    "\n",
    "(2) 私有属性和私有方法：以双下划线开头\n",
    "\n",
    "(3) 定制属性和定制方法：以双下划线开头和双下划线结尾"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`__init__` 构造函数，通过类名调用\n",
    "\n",
    "`__str__` 通过str函数调用\n",
    "\n",
    "`__len__` 通过len函数调用\n",
    "\n",
    "......"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(4) 类属性和类方法：在`__init__`**外部定义**的为类属性，**第一个参数不是self参数**的方法为类方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No deal,no hurt!\n",
      "giao is running...\n",
      "giao is running...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Animal(object):\n",
    "    \n",
    "    #类属性\n",
    "    home = 'earth'\n",
    "    \n",
    "    #类方法\n",
    "    def dream():\n",
    "        print('No deal,no hurt!')\n",
    "        \n",
    "    #定制方法\n",
    "    def __str__(self): #从属于对象的，所以加self\n",
    "        return f'An animal named {self.name}'\n",
    "        \n",
    "    #类的构造函数__init__\n",
    "    def __init__(self,name,weight,breed,age):\n",
    "        self.name = name\n",
    "        self.weight = weight\n",
    "        self.breed = breed\n",
    "        \n",
    "        # __age为私有属性\n",
    "        self.__age = age\n",
    "    \n",
    "    def run(self):\n",
    "        print(f'{self.name} is running...')\n",
    "        \n",
    "    def eat(self,food):\n",
    "        print(f'{self.name} is eating {food}...')\n",
    "        \n",
    "    def sleep(self):\n",
    "        print('Zzz...Zzz...Zzz...')\n",
    "        \n",
    "    # __think为私有方法\n",
    "    def __think(self):\n",
    "        print(\"I think I'm a hero and very handsome!\")\n",
    "        \n",
    "    # speak公有方法可以调用私有方法\n",
    "    def speak(self,words=''):\n",
    "        self.__think()\n",
    "        if words:\n",
    "            print(words)\n",
    "\n",
    "#类属性home\n",
    "gu = Animal('giao',3,'Husky',4) #实例化一个对象gu\n",
    "gu.home #输出为：'earth'\n",
    "Animal.home #输出为：'earth'\n",
    "Animal.home = 'Jupyter' #修改类属性home\n",
    "gu.home #输出为：'Jupyter'\n",
    "\n",
    "#增加对象gu的一个公有属性home，通过对象能够访问，但通过对象不能够修改类属性home\n",
    "gu.home = 'moon' \n",
    "gu.home #输出为：'moon'\n",
    "Animal.home #输出为：'Jupyter'\n",
    "del gu.home #删除对象gu的一个公有属性home\n",
    "gu.home #输出为：'Jupyter'\n",
    "\n",
    "#类方法只能通过类名访问，不能通过对象访问\n",
    "Animal.dream()\n",
    "\n",
    "#通过类名也可以访问对象方法\n",
    "Animal.run(gu) #参数self通过对象gu传递\n",
    "gu.run() \n",
    "\n",
    "#定制方法有特殊的功能，通过str调用\n",
    "str(gu) #参数self通过对象gu传递\n",
    "\n",
    "#奇妙的句点符号\n",
    "s = 'abc#123@def'\n",
    "\n",
    "#面向过程必须记住非常多的函数名，如：replace,upper,find...\n",
    "\n",
    "#面向对象只需要输入奇妙的句点符号后按下tab键\n",
    "s.replace('#','').replace('@','') #输出为：'abc123def'\n",
    "len(s) #输出：11\n",
    "s.__len__() #调用其定制方法\n",
    "\n",
    "# dir(s) #查看全部可用属性和方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2、继承"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "kitty is running...\n",
      "MiaoMiaoMiao...\n",
      "I think I'm a hero and very handsome!\n",
      "MiaoMiaoMiao...\n"
     ]
    }
   ],
   "source": [
    "# 子类可以通过继承获得父类的属性和方法\n",
    "# 可以继承多个父类，且可以增加新的属性和方法\n",
    "class Cat(Animal):\n",
    "    \n",
    "    def call(self):\n",
    "        print('MiaoMiaoMiao...') \n",
    "\n",
    "#实例化一个对象kitty\n",
    "kitty = Cat('kitty',1,'Bose',5)\n",
    "kitty.run()\n",
    "kitty.call()\n",
    "kitty.speak('MiaoMiaoMiao...')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 十四、模块和包\n",
    "\n",
    "一个.py文件就是一个模块。\n",
    "\n",
    "一个包含有`__init__`.py文件的文件夹就是一个包。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1、模块示范"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "写入一个my_nodule的模块："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting my_module.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile my_module.py \n",
    "#!/usr/bin/env python3\n",
    "#-*- coding:utf-8 -*-\n",
    "\n",
    "'a test module' #模块注释\n",
    "\n",
    "__author__ = 'HD' #作者信息\n",
    "__version__ = 'v2.0' #版本信息\n",
    "\n",
    "def __Email():  #模块私有函数\n",
    "    print('hedi1117@126.com')\n",
    "    \n",
    "def __Wechat_Official_Account():  #模块私有函数\n",
    "    print('学长说python')\n",
    "    \n",
    "def hello(person = 'world'): #公有函数\n",
    "    print('hello',person)\n",
    "    \n",
    "def test(): #测试代码\n",
    "    hello()\n",
    "    hello('China')\n",
    "    print('Contact to me:',end=' ')\n",
    "    __Email()\n",
    "    print('Learn more with me:',end=' ')\n",
    "    __Wechat_Official_Account()\n",
    "    \n",
    "\n",
    "#当直接运行该模块时，执行测试代码，而引入模块时不执行测试代码。\n",
    "if __name__ == '__main__':\n",
    "    test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#测试一下模块\n",
    "import my_module\n",
    "# help(my_module) #查看my_module.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HD'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_module.__author__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a test module'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_module.__doc__ #查看模块注释"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n",
      "hello China\n",
      "Contact to me: hedi1117@126.com\n",
      "Learn more with me: 学长说python\n"
     ]
    }
   ],
   "source": [
    "!python my_module.py #执行脚本"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2、包示范"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "建立一个Animal文件夹:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "!mkdir Animal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing Animal/__init__.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile Animal/__init__.py\n",
    "#!/usr/bin/env python3\n",
    "#-*- coding:utf-8 -*-\n",
    "\n",
    "'Animal module for fun！' #模块注释\n",
    "__author__ = 'HD' #作者信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing Animal/Animal.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile Animal/Animal.py\n",
    "class Animal(object):\n",
    "    def __init__(self,name):\n",
    "        self.name = name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing Animal/Cat.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile Animal/Cat.py\n",
    "from Animal.Animal import Animal\n",
    "class Cat(Animal):\n",
    "    def call(self):\n",
    "        print('miaoao...miaoao...miaoao...')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing Animal/Dog.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile Animal/Dog.py\n",
    "from Animal.Animal import Animal\n",
    "class Dog(Animal):\n",
    "    def call(self):\n",
    "        print('Bowwow...Bowwow...Bowwow...')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "#测试一下包\n",
    "import Animal\n",
    "#help(Animal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bowwow...Bowwow...Bowwow...\n"
     ]
    }
   ],
   "source": [
    "from Animal.Dog import Dog\n",
    "gu = Dog('giao')\n",
    "gu.call()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "miaoao...miaoao...miaoao...\n"
     ]
    }
   ],
   "source": [
    "from Animal.Cat import Cat\n",
    "kitty = Cat('kitty')\n",
    "kitty.call()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc-autonumbering": false,
  "toc-showcode": true,
  "toc-showmarkdowntxt": false,
  "toc-showtags": false
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
